package card;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2023-09-26
 * Time: 15:19
 */
public class CardDemo {
    private final String[] suits = {"♥","♠","♦","♣"};


    //生成一副扑克牌(没有大小王)
    public List<Card> buyCards() {
        List<Card> cardList = new ArrayList<>();

        //装牌:4种花色,每种花色有13张牌
        for (int i = 0; i < suits.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i],j);
                cardList.add(card);
            }
        }

        return cardList;
    }

    //洗牌
    public void shuffle(List<Card> cardList) {

        //得到两个要交换的牌
        for (int i = cardList.size() - 1; i > 0; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
            swap(cardList,index,i);
        }
    }

    //交换牌的位置
    public void swap(List<Card> cardList,int index,int i) {
        Card tmp;

        tmp = cardList.get(index);
        cardList.set(index,cardList.get(i));
        cardList.set(i,tmp);
    }

    //抓牌
    public List<List<Card>> garbCards(List<Card> cardList) {
        List<List<Card>> hands = new ArrayList<>();

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        //三个人轮流抓 5张牌
        for (int i = 0; i < 3; i++) {
            List<Card> hand = hands.get(i);
            for (int j = 0; j < 5; j++) {
                Card card = cardList.remove(0);
                hand.add(card);
            }
        }

        return hands;
    }
}
